<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="service.ShenqingService" %>
<%@ page import="service.VehicleService" %>
<%@ page import="service.DriversService" %>
<%@ page import="beans.Shenqing" %>
<%@ page import="beans.Vehicle" %>
<%@ page import="beans.Drivers" %>
<%@ page import="java.util.List" %>
<%
    // 检查session是否存在
    if(session.getAttribute("username") == null) {
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return;
    }
    String username = (String)session.getAttribute("username");
    String role = (String)session.getAttribute("role");
    
    // 获取申请ID
    String orderId = request.getParameter("orderId");
    ShenqingService shenqingService = new ShenqingService();
    VehicleService vehicleService = new VehicleService();
    DriversService driverService = new DriversService();
    
    Shenqing shenqing = null;
    List<Vehicle> vehicleList = null;
    List<Drivers> driverList = null;
    
    try {
        shenqing = shenqingService.getShenqingById(orderId);
        vehicleList = vehicleService.getAllAvailableVehicles();
        driverList = driverService.getAllAvailableDrivers();
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>分配司机和车辆 - 公司用车管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
            color: #333;
        }
        .header {
            background: #183a8a;
            color: #fff;
            padding: 18px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;
        }
        .header h1 {
            flex-grow: 1;
            text-align: center;
            margin: 0;
            font-size: 2rem;
        }
        .container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #183a8a;
        }
        select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .button-group {
            margin-top: 30px;
            display: flex;
            gap: 15px;
            justify-content: center;
        }
        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
        }
        .btn-primary {
            background: #28a745;
            color: white;
        }
        .btn-primary:hover {
            background: #218838;
        }
        .btn-secondary {
            background: #6c757d;
            color: white;
            text-decoration: none;
        }
        .btn-secondary:hover {
            background: #5a6268;
        }
        .info-section {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
        }
        .info-item {
            margin-bottom: 10px;
        }
        .info-item label {
            display: inline-block;
            width: 120px;
            margin-right: 10px;
        }
        .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            min-height: 100px;
            resize: vertical;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>公司用车管理系统</h1>
    </div>

    <div class="container">
        <h2>分配司机和车辆</h2>
        
        <% if (shenqing != null) { %>
            <div class="info-section">
                <div class="info-item">
                    <label>申请编号：</label>
                    <span><%= shenqing.getOrderId() %></span>
                </div>
                <div class="info-item">
                    <label>申请人：</label>
                    <span>员工<%= shenqing.getEmployeeId() %></span>
                </div>
                <div class="info-item">
                    <label>用车时间：</label>
                    <span><%= shenqing.getDepartureTime() %></span>
                </div>
                <div class="info-item">
                    <label>预计归还时间：</label>
                    <span><%= shenqing.getReturnTime() %></span>
                </div>
                <div class="info-item">
                    <label>用车事由：</label>
                    <span><%= shenqing.getUsageReason() %></span>
                </div>
                <div class="info-item">
                    <label>目的地：</label>
                    <span><%= shenqing.getDestination() %></span>
                </div>
                <div class="info-item">
                    <label>随行人员：</label>
                    <span><%= shenqing.getPassengers() %></span>
                </div>
            </div>

            <form action="<%=request.getContextPath()%>/ShenqingServlet" method="post">
                <input type="hidden" name="action" value="approve">
                <input type="hidden" name="orderId" value="<%=shenqing.getOrderId()%>">
                <input type="hidden" name="employeeId" value="<%=shenqing.getEmployeeId()%>">
                <input type="hidden" name="departureTime" value="<%=shenqing.getDepartureTime()%>">
                <input type="hidden" name="returnTime" value="<%=shenqing.getReturnTime()%>">
                <input type="hidden" name="usageReason" value="<%=shenqing.getUsageReason()%>">
                <input type="hidden" name="passengers" value="<%=shenqing.getPassengers()%>">
                <input type="hidden" name="destination" value="<%=shenqing.getDestination()%>">
                <input type="hidden" name="reviewer" value="<%=username%>">
                
                <div class="form-group">
                    <label for="vehicleId">选择车辆：</label>
                    <select id="vehicleId" name="vehicleId" required>
                        <option value="">--请选择车辆--</option>
                        <% 
                        if (vehicleList != null) {
                            System.out.println("车辆列表大小: " + vehicleList.size());
                            for (Vehicle vehicle : vehicleList) { 
                                System.out.println("处理车辆: " + vehicle.getLicensePlate() + " - " + vehicle.getBrandModel());
                        %>
                                <option value="<%=vehicle.getVehicleId()%>">
                                    <%=vehicle.getLicensePlate()%> - <%=vehicle.getBrandModel()%>
                                </option>
                        <% 
                            }
                        } else {
                            System.out.println("vehicleList 为 null");
                        }
                        %>
                    </select>
                </div>

                <div class="form-group">
                    <label for="driverId">选择司机：</label>
                    <select id="driverId" name="driverId" required>
                        <option value="">--请选择司机--</option>
                        <% if (driverList != null) {
                            for (Drivers driver : driverList) { %>
                                <option value="<%=driver.getDriverId()%>">
                                    <%=driver.getDriverName()%> - <%=driver.getPhoneNumber()%>
                                </option>
                            <% }
                        } %>
                    </select>
                </div>

                <div class="form-group">
                    <label for="statusremarks">审核备注：</label>
                    <textarea id="statusremarks" name="statusremarks" placeholder="请输入审核备注信息..."></textarea>
                </div>

                <div class="button-group">
                    <button type="submit" class="btn btn-primary">确认分配</button>
                    <a href="ShenqingDetail.jsp?orderId=<%=shenqing.getOrderId()%>" class="btn btn-secondary">返回</a>
                </div>
            </form>
        <% } else { %>
            <div class="error-message">
                <p>未找到申请信息或无权查看此申请</p>
            </div>
            <div class="button-group">
                <a href="ShenqingServlet?action=all" class="btn btn-secondary">返回列表</a>
            </div>
        <% } %>
    </div>
</body>
</html> 